﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.School;
using SIMS.Global;
using SIMS.Company;

public partial class School_CourseDetail_CreateEdit : System.Web.UI.Page
{
    private void BindCourseList()
    {
        ddlCourseList.Items.Clear();
        ddlCourseList.Items.Add(new ListItem("Select", "-1"));

        CourseList _courseList = new CourseList();
        CourseDB _courseDB = new CourseDB();
        _courseList = _courseDB.GetCourseList(Convert.ToInt32(hidCompanyId.Value));
        if (_courseList != null && _courseList.Count > 0)
        {
            foreach (Course course in _courseList)
            {
                ddlCourseList.Items.Add(new ListItem(course.Name, course.Id.ToString()));
            }
        }
    }

    private void BindMajorFieldList()
    {
        ddlMajorFieldList.Items.Clear();
        ddlMajorFieldList.Items.Add(new ListItem("Select", "-1"));

        MajorFieldList _majorFieldList = new MajorFieldList();
        MajorFieldDB _majorFieldDB = new MajorFieldDB();
        _majorFieldList = _majorFieldDB.GetMajorFieldList(Convert.ToInt32(hidCompanyId.Value));
        if (_majorFieldList != null && _majorFieldList.Count > 0)
        {
            foreach (MajorField _majorField in _majorFieldList)
            {
                ddlMajorFieldList.Items.Add(new ListItem(_majorField.Name, _majorField.Id.ToString()));
            }
        }
    }

    private void BindStandardLevelList()
    {
        ddlStandardLevelList.Items.Clear();
        ddlStandardLevelList.Items.Add(new ListItem("Select", "-1"));

        StandardLevelList _StandardLevelList = new StandardLevelList();
        StandardLevelDB _StandardLevelDB = new StandardLevelDB();
        _StandardLevelList = _StandardLevelDB.GetStandardLevelList(Convert.ToInt32(hidCompanyId.Value));
        if (_StandardLevelList != null && _StandardLevelList.Count > 0)
        {
            foreach (StandardLevel _StandardLevel in _StandardLevelList)
            {
                ddlStandardLevelList.Items.Add(new ListItem(_StandardLevel.Name, _StandardLevel.Id.ToString()));
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session[Global.CompanyId] != null)
            {
                hidCompanyId.Value = Session[Global.CompanyId].ToString();
                lblCompany.Text = Session[Global.CompanyName].ToString();
                BindCourseList();
                BindMajorFieldList();
                BindStandardLevelList();

                if (Request.Params["id"] != null)
                {
                    int _CourseDetailId = 0;
                    if (int.TryParse(Request.Params["id"].ToString(), out _CourseDetailId) && _CourseDetailId > 0)
                    {
                        CourseDetail _CourseDetail = new CourseDetail();
                        CourseDetailDB _CourseDetailDB = new CourseDetailDB();
                        _CourseDetail = _CourseDetailDB.GetCourseDetail(_CourseDetailId);

                        if (_CourseDetail.Id > 0)
                        {
                            if (_CourseDetail.Error.ErrorNo > 0)
                            {
                                lblMessage.Text = _CourseDetail.Error.ErrorMessage;
                            }
                            else
                            {
                                hidCourseDetailId.Value = _CourseDetailId.ToString();
                                txtCourseDetailName.Text = _CourseDetail.Name;
                                txtDescription.Text = _CourseDetail.Description;
                                ddlCourseList.Text = _CourseDetail.CourseID.ToString();
                                ddlMajorFieldList.Text = _CourseDetail.MajorFieldID.ToString();
                                ddlStandardLevelList.Text = _CourseDetail.StandardLevelID.ToString();
                            }
                        }
                    }
                    else
                    {
                        lblMessage.Text = "Invalid parameter!";
                    }
                }
            }
            else
            {
                lblMessage.Text = "No company found!";
                btnSubmit.Enabled = false;
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {
                CourseDetail CourseDetail = new CourseDetail();
                CourseDetailDB _CourseDetailDB = new CourseDetailDB();
                int _CourseDetailId = 0;

                if (int.TryParse(hidCourseDetailId.Value, out _CourseDetailId) && _CourseDetailId > 0)
                {
                    CourseDetail = _CourseDetailDB.GetCourseDetail(_CourseDetailId);
                }
                else
                {
                    CourseDetail.Id = 0;
                    CourseDetail.Error = new MyError();
                }

                CourseDetail.Name = txtCourseDetailName.Text;

                if (txtDescription.Text.Trim().Length > 0)
                {
                    CourseDetail.Description = txtDescription.Text;
                }
                else
                {
                    CourseDetail.Description = string.Empty;
                }

                CourseDetail.CourseID = Convert.ToInt32(ddlCourseList.Text);
                CourseDetail.StandardLevelID = Convert.ToInt32(ddlStandardLevelList.Text);
                CourseDetail.MajorFieldID = Convert.ToInt32(ddlMajorFieldList.Text);

                CourseDetail.IsActive = false;

                _CourseDetailDB.Save(CourseDetail);

                if (CourseDetail.Error.ErrorNo > 0)
                {
                    if (CourseDetail.Error.ErrorNo == 1)
                    {
                        lblMessage.Text = CourseDetail.Error.ErrorMessage;
                    }
                    else
                    {
                        lblMessage.Text = "(" + CourseDetail.Error.ErrorLocation + ")";
                        lblMessage.Text += CourseDetail.Error.ErrorMessage;
                        lblMessage.Text += "<br/><br/>" + CourseDetail.Error.ErrorStack;
                    }
                }
                else
                {
                    lblMessage.Text = "Saved successfully!";
                    Response.Redirect("default.aspx?companyid=" + hidCompanyId.Value);
                }
            }
            else
            {
                lblMessage.Text = "Invalid form.";
            }
        }
        catch (Exception Ex)
        {
            lblMessage.Text = Ex.Message;
            MyError.CreateExceptionLog(Ex, "Company_CreateEdit");
        }

    }

}